﻿'use strict';
app
    // Login Box controller 64800
    .controller('PinCodeController', [
        '$rootScope', '$scope', '$state', '$localStorage', '$http', '$modal', '$timeout', function ($rootScope, $scope, $state, $localStorage, $http, $modal, $timeout) {
            $scope.showbtnResent = false,
            $timeout(function () {
                $scope.showbtnResent = true;
            }, 3600),
            $scope.popup = function (windowClass, templateUrl) {
                $modal.open({
                    windowClass: windowClass,
                    templateUrl: templateUrl,
                    controller: "PinCodeModalInstanceCtrl",
                    size: "sm",
                    resolve: {
                        messeger: function () {
                            return $scope.messeger;
                        },
                        title: function () {
                            return $scope.title;
                        },
                        Pincode: function () {
                            return $scope.Pincode;
                        }
                    }
                })
            },
            $scope.doResetPass = function (Pincode) {
                $http.post($rootScope.APIUrl + 'ResetPass', { pincode: Pincode })
                    .then(
                    function successCallback(resp) {
                        if (resp.data.Status) {
                            $scope.Pincode = Pincode;
                            $scope.popup('modal-message', "PasswordReset");
                        } else {
                            $scope.messeger = resp.data.Messeger,
                            $scope.title = 'Warning Error',
                            $scope.popup('modal-message modal-warning', "WarningModal");
                        }
                    }, function errorCallback(resp) {
                        $scope.checkSponsor = 0;
                        $scope.messeger = "Contact the Administrator by supporting",
                        $scope.title = 'Warning Error',
                        $scope.popup('modal-message modal-warning', "WarningModal");
                    })
            },
            $scope.doResentPinCode = function () {
                $scope.showbtnResent = false,
                $http.post($rootScope.APIUrl + 'Forgotpass', { mail: $localStorage.MailPinCode }).then(
                    function successCallback(resp) {
                        if (resp.data.Status) {
                            $timeout(function () {
                                $scope.showbtnResent = true;
                            }, 64800),
                            $scope.title = 'Pincode Resend',
                            $scope.messeger = resp.data.Messeger,
                            $scope.popup('modal-message  modal-success', "WarningModal");
                        } else {
                            $scope.messeger = resp.data.Messeger,
                            $scope.title = 'Warning Error',
                            $scope.popup('modal-message modal-warning', "WarningModal");
                        }
                    }, function errorCallback(resp) {
                        if (resp.status == 0) {
                            $scope.messeger = "Contact the Administrator by Supporting",
                            $scope.title = 'Resent Pincode Error',
                            $scope.popup('modal-message modal-warning', "WarningModal");
                        }
                    })
            };

            console.log($scope.showbtnResent);
        }
    ])

app.controller('PinCodeModalInstanceCtrl', function ($scope, $rootScope, $modalInstance, $http, $modal, messeger, title, Pincode) {
    $scope.messeger = messeger,
    $scope.title = title,
    $scope.Pincode = Pincode,
    $scope.ok = function () {
        $modalInstance.close();
    },
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    },
    $scope.doPasswordReset = function (Password) {
        $scope.messeger = "";
        // An elaborate, custom popup
        $http.post($rootScope.APIUrl + 'ChangePassByPincode', { pincode: Pincode, password: Password })
            .then(function successCallback(resp) {
                if (resp.data.Status) { 
                    $modalInstance.close();                   
                    
                } 
                $scope.messeger = resp.data.Messeger,                
                $modal.open({
                    windowClass: "modal-message modal-success",
                    templateUrl: "WarningModal",
                    controller: "ModalInstanceCtrl",
                    size: "sm",
                    resolve: {
                        messeger: function () {
                            return resp.data.Messeger;
                        },
                        title: function () {
                            return "Change Password";
                        },
                    }
                })

            }, function errorCallback(resp) {
                if (resp.status == 0) {
                    $modal.open({
                        windowClass: "modal-message modal-warning",
                        templateUrl: "WarningModal",
                        controller: "ModalInstanceCtrl",
                        size: "sm",
                        resolve: {
                            messeger: function () {
                                return "Contact the Administrator by Supporting";
                            },
                            title: function () {
                                return "Change Password";
                            },                          
                        }})
                }
            })
    };
}),
app.controller('ModalInstanceCtrl', function ($scope, $modalInstance, $state, messeger, title) {
    $scope.messeger = messeger,
    $scope.title = title;
    $scope.ok = function () {
        $state.go('login'),
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});


